#ifndef RECEIVE_MSG_CALLABLE_H_
#define RECEIVE_MSG_CALLABLE_H_
#include <boost/shared_ptr.hpp>
#include <thrift/protocol/TBinaryProtocol.h>
#include <thrift/protocol/TDebugProtocol.h>
#include <thrift/transport/TSocket.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/transport/THttpClient.h>
#include <thrift/TProcessor.h>
#include <glog/logging.h>
#include "gen-cpp/ChatService.h"
#include "client/RequestQueue.h"
namespace at = apache::thrift;
namespace atp = apache::thrift::protocol;
namespace att = apache::thrift::transport;

namespace client {
  class ReceiveMsgCallable {
 public:
 ReceiveMsgCallable(boost::shared_ptr<at::TProcessor> processor, boost::shared_ptr<atp::TProtocol> prot) : processor_(processor), prot_(prot) {
    }
    void operator()() {
      for(;;) {
        try {
          if (!processor_->process(prot_, prot_, NULL) ||
              !prot_->getTransport()->peek()) {
            LOG(WARNING) << "process error or transport peek error";
          }
        }
        catch (const att::TTransportException& tte) {
          LOG(ERROR) << tte.what();
          break;
        }
        catch (const std::exception& e) {
          LOG(WARNING) << e.what();
        }
        LOG(INFO) << "schedule empty request";
        RequestQueue::Instance()->Schedule(chat::SendEmptyRequestReq());
      }

      LOG(INFO) << "ReceiveMsg thread exit";
    }

 private:
    boost::shared_ptr<at::TProcessor> processor_;
    boost::shared_ptr<atp::TProtocol> prot_;
  };
}
#endif
